package com.erlinyou.im.web.controller.base;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.RenderingHints;
import java.awt.geom.AffineTransform;
import java.awt.geom.Ellipse2D;
import java.awt.image.AffineTransformOp;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import javax.imageio.ImageIO;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public abstract class ImgUtils {
	private static Logger logger = LoggerFactory.getLogger(ImgUtils.class);
	    /**
	     */
	    private static final String PICTRUE_FORMATE = "jpg";

	    /**
	     *
	     * @param paths 杈撳叆娴�
	     * @param outPath  
	     * @throws IOException
	     */
	    public static BufferedImage generate(List<BufferedImage> streams) throws IOException {
	        BufferedImage image = null;
			if(streams.size()==3) {
				image = generate(streams, 160, 4);
			}else if(streams.size()==4) {
				image = generate(streams, 175, 4);
			}else if(streams.size()==5) {
				image = generate(streams, 148, 4);
		    }else if(streams.size()==2) {
				image = generate(streams, 145, 4);
		    }
				return image;
		}

	    /**
	     * 生成组合头像
	     *
	     * @param paths    用户头像路径列表
	     * @param length   画板的宽高和高度
	     * @param interval 画板中的图片间距
	     * @param outPath  生成后的头像保存路径
	     * @throws IOException
	     */
	    public static BufferedImage generate( List<BufferedImage> bufferedImages, int length, int interval) throws IOException {
	        int wh = (length - interval * 4) / 3; 
	        if (bufferedImages.size() == 1) {
	            wh = length - interval * 2; 
	        }
	        if (bufferedImages.size() > 1 && bufferedImages.size() < 5) {
	            wh = (length - interval * 3) / 2; 
	        }
		/*
		 * for (int i = 0; i < bufferedImages.size(); i++) { ByteArrayOutputStream os =
		 * new ByteArrayOutputStream(); ImageIO.write(bufferedImages.get(i), "jpg", os);
		 * InputStream is = new ByteArrayInputStream(os.toByteArray());
		 * bufferedImages.add(ImgUtils.resize(is, wh, wh, false)); }
		 */
	        BufferedImage outImage = new BufferedImage(length, length, BufferedImage.TYPE_INT_RGB);
	        Graphics g = outImage.getGraphics();
	        Graphics2D g2d = (Graphics2D) g;
	        g2d.setBackground(new Color(255, 255, 255));
	        g2d.clearRect(0, 0, length, length);
	        for (int i = 1; i <= bufferedImages.size(); i++) {
	            int j = i % 3 + 1;
	            if (bufferedImages.size() < 5) {
	                j = i % 2 + 1;
	            }
	            int x = interval * j + wh * (j - 1);
	            int split = (wh + interval) / 2;
	            if (bufferedImages.size() == 9) {
	                if (i <= 3) {
	                    g2d.drawImage(bufferedImages.get(i - 1), x, wh * 2 + interval * 3, null);
	                } else if (i <= 6) {
	                    g2d.drawImage(bufferedImages.get(i - 1), x, wh + interval * 2, null);
	                } else {
	                    g2d.drawImage(bufferedImages.get(i - 1), x, interval, null);
	                }
	            } else if (bufferedImages.size() == 8) {
	                if (i <= 3) {
	                    g2d.drawImage(bufferedImages.get(i - 1), x, wh * 2 + interval * 3, null);
	                } else if (i <= 6) {
	                    g2d.drawImage(bufferedImages.get(i - 1), x, wh + interval * 2, null);
	                } else {
	                    g2d.drawImage(bufferedImages.get(i - 1), x - split, interval, null);
	                }
	            } else if (bufferedImages.size() == 7) {
	                if (i <= 3) {
	                    g2d.drawImage(bufferedImages.get(i - 1), x, wh * 2 + interval * 3, null);
	                } else if (i <= 6) {
	                    g2d.drawImage(bufferedImages.get(i - 1), x, wh + interval * 2, null);
	                } else {
	                    g2d.drawImage(bufferedImages.get(i - 1), (length - wh) / 2, interval, null);
	                }
	            } else if (bufferedImages.size() == 6) {
	                if (i <= 3) {
	                    g2d.drawImage(bufferedImages.get(i - 1), x, wh * 2 + interval * 3 - split, null);
	                } else if (i <= 6) {
	                    g2d.drawImage(bufferedImages.get(i - 1), x, wh + interval * 2 - split, null);
	                }
	            } else if (bufferedImages.size() == 5) {
	            	switch (String.valueOf(i)) {
	            	case "1":
						g2d.drawImage(bufferedImages.get(4), 41, 2, null);//
						break;
					case "2":
						g2d.drawImage(bufferedImages.get(1), 76, 32, null);
						break;
					case "3":
						g2d.drawImage(bufferedImages.get(3), 66, 77, null);//
						break;
					case "4":
						g2d.drawImage(bufferedImages.get(2), 16, 77, null);
						break;
					case "5":
						g2d.drawImage(bufferedImages.get(0), 4, 32, null);//
						break;
					default:
						break;
					}
	            	
	            	/*
	                if (i <= 3) {
	                    g2d.drawImage(bufferedImages.get(i - 1), x, wh * 2 + interval * 3 - split, null);
	                } else {
	                    g2d.drawImage(bufferedImages.get(i - 1), x - split, wh + interval * 2 - split, null);
	                }
	                */
	            } else if (bufferedImages.size() == 4) {
	            	switch (String.valueOf(i)) {
					case "1":
						g2d.drawImage(bufferedImages.get(0), 13, 10, null);
						break;
					case "2":
						g2d.drawImage(bufferedImages.get(1), 73, 10, null);
						break;
					case "3":
						g2d.drawImage(bufferedImages.get(3), 13, 70, null);
						break;
					case "4":
						g2d.drawImage(bufferedImages.get(2), 73, 70, null);
						break;
					default:
						break;
					}
	            } else if (bufferedImages.size() == 3) {
	            	switch (String.valueOf(i)) {
					case "1":
						g2d.drawImage(bufferedImages.get(0), 35, 1, null);
						break;
					case "2":
						g2d.drawImage(bufferedImages.get(2), 5, 57, null);
						break;
					case "3":
						g2d.drawImage(bufferedImages.get(1), 70, 57, null);
						break;
					default:
						break;
					}
	            } else if (bufferedImages.size() == 2) {
	            	switch (String.valueOf(i)) {
					case "1":
						g2d.drawImage(bufferedImages.get(1), 2, 30, null);
						break;
					case "2":
						g2d.drawImage(bufferedImages.get(0), 57, 30, null);
						break;
					default:
						break;
					}
	            } else if (bufferedImages.size() == 1) {
	                g2d.drawImage(bufferedImages.get(0), 30, 30, null);
	            }
	        }
	       // ImageIO.write(outImage, PICTRUE_FORMATE, new File(outPath));
	    	BufferedImage convertImage = null;
			try {
				convertImage = scaleByPercentage(outImage, 200, 200);
			} catch (Exception e) {
				e.printStackTrace();
				logger.error("Image change error"+e);
			}
	    	return convertImage = convertCircular(convertImage);
	    }


	    public static BufferedImage resize(String filePath, int width, int height) {
	        try {
	            BufferedImage bi = ImageIO.read(new File(filePath));
	            bi.getSubimage(0, 0, width, height);
	        } catch (IOException e) {
	            e.printStackTrace();
	        }
	        return null;
	    }

	    /**
	     * 图片锟斤拷锟斤拷
	     *
	     * @param filePath 图片路锟斤拷
	     * @param height   锟竭讹拷
	     * @param width    锟斤拷锟�
	     * @param bb       锟斤拷锟斤拷锟斤拷锟斤拷时锟角凤拷锟斤拷要锟斤拷锟斤拷
	     * @throws Exception 
	     */
	    public static BufferedImage resize(InputStream stream, int height, int width, boolean bb) throws Exception {
	        try {
	            double ratio = 0; // 锟斤拷锟脚憋拷锟斤拷
//	            File f = new File(filePath);
	            BufferedImage bi = ImageIO.read(stream);
	            Image itemp = bi.getScaledInstance(width, height, Image.SCALE_SMOOTH);
	            // 锟斤拷锟斤拷锟斤拷锟�
	            if ((bi.getHeight() > height) || (bi.getWidth() > width)) {
	                if (bi.getHeight() > bi.getWidth()) {
	                    ratio = (new Integer(height)).doubleValue() / bi.getHeight();
	                } else {
	                    ratio = (new Integer(width)).doubleValue() / bi.getWidth();
	                }
	                AffineTransformOp op = new AffineTransformOp(AffineTransform.getScaleInstance(ratio, ratio), null);
	                itemp = op.filter(bi, null);
	            }
	            if (bb) {
	                BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
	                Graphics2D g = image.createGraphics();
	                g.setColor(Color.white);
	                g.fillRect(0, 0, width, height);
	                if (width == itemp.getWidth(null)) {
	                    g.drawImage(itemp, 0, (height - itemp.getHeight(null)) / 2, itemp.getWidth(null), itemp.getHeight(null), Color.white, null);
	                } else {
	                    g.drawImage(itemp, (width - itemp.getWidth(null)) / 2, 0, itemp.getWidth(null), itemp.getHeight(null), Color.white, null);
	                }
	                g.dispose();
	                itemp = image;
	            }
	            
	            return (BufferedImage) itemp;
	        } catch (IOException e) {
	            e.printStackTrace();
	        }finally {
	        	stream.close();
	        }
	        return null;
	    }

	    public static BufferedImage convertCircular(BufferedImage bi1) throws IOException {
			
    		//		BufferedImage bi1 = ImageIO.read(new File(url));
    				
    				// 这种是黑色底的
    		//		BufferedImage bi2 = new BufferedImage(bi1.getWidth(), bi1.getHeight(), BufferedImage.TYPE_INT_RGB);
    		 
    				// 透明底的图片
    				BufferedImage bi2 = new BufferedImage(bi1.getWidth(), bi1.getHeight(), BufferedImage.TYPE_4BYTE_ABGR);
    				Ellipse2D.Double shape = new Ellipse2D.Double(0, 0, bi1.getWidth(), bi1.getHeight());
    				Graphics2D g2 = bi2.createGraphics();
    				g2.setClip(shape);
    				// 使用 setRenderingHint 设置抗锯齿
    				g2.drawImage(bi1, 0, 0, null);
    				// 设置颜色
    				g2.setBackground(Color.green);
    				g2.dispose();
    				return bi2;
    			}
    		
    	public static BufferedImage scaleByPercentage(BufferedImage inputImage, int newWidth, int newHeight) throws Exception {
    			// 获取原始图像透明度类型
    			int type = inputImage.getColorModel().getTransparency();
    			int width = inputImage.getWidth();
    			int height = inputImage.getHeight();
    			// 开启抗锯齿
    			RenderingHints renderingHints = new RenderingHints(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    			// 使用高质量压缩
    			renderingHints.put(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
    			BufferedImage img = new BufferedImage(newWidth, newHeight, type);
    			Graphics2D graphics2d = img.createGraphics();
    			graphics2d.setRenderingHints(renderingHints);
    			graphics2d.drawImage(inputImage, 0, 0, newWidth, newHeight, 0, 0, width, height, null);
    			graphics2d.dispose();
    			return img;
    	}
	    
	    public static void main(String[] args) throws Exception {
	        ArrayList<String> picUrls = new ArrayList<>();
	        String pic = "C:\\Users\\Public\\Pictures\\Sample Pictures\\";
	        picUrls.add(pic + "1.jpg");
	        picUrls.add(pic + "2.jpg");
	        picUrls.add(pic + "3.jpg");
	        picUrls.add(pic + "4.jpg");
	        picUrls.add(pic + "5.jpg");
	        picUrls.add(pic + "6.jpg");
	        picUrls.add(pic + "7.jpg");
	        picUrls.add(pic + "8.jpg");
	        picUrls.add(pic + "9.jpg");

	        //2 锟斤拷锟矫癸拷锟斤拷锟斤拷 锟斤拷锟缴九癸拷锟斤拷 锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷路锟斤拷
//	        ImgUtils.generate(picUrls, "D:\\imgutil.jpg");
	    }
	}
